﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.UI.HtmlControls;
using CompanyBussiness;

namespace Website.customer.Control
{
    public partial class cListNewsByCategory : System.Web.UI.UserControl
    {
        HtmlMeta metaTag;
        NewsManagement newsMana = new NewsManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        TurnOverService overServices = new TurnOverService();
        CompanyBussiness.New news = new New();
        CompanyBussiness.Language language;
        Metadata meta;
        public int CurPage = 1;
        int TotalRow;
        int TotalPage;
        int StartRow = 0;
        int categoryIDs;
        string modulCode;
        bool searchWithDate = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            modulCode = Request.QueryString["Mod"];
            //loadModul();
            categoryIDs = int.Parse(Request[CONST.QUERYSTRING_CATEGORY].ToString());

            if (Request.Cookies[CONST.COOKIES_LANGUAGE] == null)
            {
                Session[CONST.SESSION_LANGUAGE] = "";
                language = languageMana.GetLanguageByCode("vi-VN");
            }
            else
            {
                Session[CONST.SESSION_LANGUAGE] = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
                language = languageMana.GetLanguageByID(int.Parse(Session[CONST.SESSION_LANGUAGE].ToString()));
            }

            if (!Page.IsPostBack)
            {
                if (Request[CONST.QUERYSTRING_CATEGORY] == null && Request[CONST.QUERYSTRING_SUBJECT_NEWS]==null)
                {
                    //quay ve trang home
                }
                else if (Request[CONST.QUERYSTRING_CATEGORY] != null)
                {
                    categoryIDs = int.Parse(Request[CONST.QUERYSTRING_CATEGORY].ToString());
                    lbNameCategory.Text = newsMana.GetCategoryByID(categoryIDs, modulCode) !=null ? newsMana.GetCategoryByID(categoryIDs, modulCode).CategoryName : "";
                    this.LoadDataList(int.Parse(ConfigurationManager.AppSettings["NumberPostInPage"]));

                    meta = newsMana.GetMetadataByCategoryID(categoryIDs);
                    if (meta != null)
                    {
                        //ghi metadata
                        metaTag = new HtmlMeta();
                        metaTag.Name = "keywords";
                        metaTag.Content = meta.MetaKeyword;
                        Page.Header.Controls.Add(metaTag);

                        metaTag = new HtmlMeta();
                        metaTag.Name = "description";
                        metaTag.Content = meta.MetaDesciption;
                        Page.Header.Controls.Add(metaTag);

                        Page.Header.Title = meta.Title;
                    }
                }
                else if (Request[CONST.QUERYSTRING_SUBJECT_NEWS] != null)
                {
                    lbNameCategory.Text = "";
                    searchWithDate = true;
                }
            }
        }


        protected void loadModul(string ids)
        {
            switch (modulCode)
            {
                case "MNEWS":
                    {
                        //Response.Redirect("Newsroom.aspx?" + CONST.QUERYSTRING_NEWS + "=" + ids + "&Mod=MNEWS");
                        Response.Redirect("~/Hem/"+ids+"_"+categoryIDs+"/tin-tuc/"+ "MNEWS" + "/" + overServices.ConvertString(newsMana.GetNameNewsByID(int.Parse(ids)).Replace(" ","-")) + ".html");
                    }
                    break;
                case "MSHAREHOLDERS":
                    {
                        //Response.Redirect("Shareholder.aspx?" + CONST.QUERYSTRING_NEWS + "=" + ids + "&Mod=MSHAREHOLDERS");
                        Response.Redirect("~/Hem/" + ids + "_" + categoryIDs + "/quan-he-co-dong/" + "MSHAREHOLDERS" + "/" + overServices.ConvertString(newsMana.GetNameNewsByID(int.Parse(ids)).Replace(" ", "-")) + ".html");
                    }
                    break;
                case "MJOB":
                    {
                        //Response.Redirect("Job.aspx?" + CONST.QUERYSTRING_NEWS + "=" + ids + "&Mod=MJOB");
                        Response.Redirect("~/Hem/" + ids + "_" + categoryIDs + "/tuyen-dung/" + "MJOB" + "/" + overServices.ConvertString(newsMana.GetNameNewsByID(int.Parse(ids)).Replace(" ", "-")) + ".html");
                    }
                    break;
                case "MHELP":
                    {
                        //Response.Redirect("Help.aspx?" + CONST.QUERYSTRING_NEWS + "=" + ids + "&Mod=MHELP");
                        Response.Redirect("~/Hem/" + ids + "_" + categoryIDs + "/tro-giup/" + "MHELP" + "/" + overServices.ConvertString(newsMana.GetNameNewsByID(int.Parse(ids)).Replace(" ", "-")) + ".html");
                    }
                    break;
                case "MCONTACT":
                    {

                    }
                    break;
                case "MINTRO":
                    {
                        //Response.Redirect("AboutUs.aspx?" + CONST.QUERYSTRING_NEWS + "=" + ids + "&Mod=MINTRO");
                    }
                    break;
                case "MPRODUCT":
                    {

                    }
                    break;
            }


        }

        protected void LoadDataList(int take)
        {
            try
            {
                if (Request["Page"] == null)
                {
                    CurPage = 1;
                }
                else
                {
                    CurPage = Convert.ToInt32(Request["Page"]);
                }

                if (CurPage > 1)
                {
                    StartRow = (CurPage - 1) * 10;
                }
                else
                {
                    StartRow = 0;
                }

                if (searchWithDate==true)   //có lọc ngày bắt đầu
                {
                    dataListCategoryNews.DataSource = newsMana.GetListNewsSearch(Request[CONST.QUERYSTRING_SUBJECT_NEWS].ToString(),take,StartRow);
                    dataListCategoryNews.DataBind();
                    TotalRow = newsMana.GetListNewsSearch(Request[CONST.QUERYSTRING_SUBJECT_NEWS].ToString(), take, StartRow).Count();
                }
                else
                {
                    dataListCategoryNews.DataSource = newsMana.GetNewsByParentCategory(categoryIDs, take, StartRow);
                    dataListCategoryNews.DataBind();
                    TotalRow = newsMana.GetNewsByParentCategory(categoryIDs, take, StartRow).Count();
                }

                if (TotalRow == 0)
                {
                    lnkNext.Visible = false;
                    lnkPrev.Visible = false;
                    lnkNext.Text = "";
                    lnkPrev.Text = "";
                    lbNameCategory.Text = languageMana.GetTagValueByLanguageIDandTagName("NotfoundNews", language.LanguagesID);
                    return;
                }
                else
                {

                }

                this.Pagination(take);
            }
            catch (Exception)
            {

            }
        }

        protected void LinkTitle_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                Session[CONST.SESSION_NEWS] = Convert.ToInt32(e.CommandArgument);
                try
                {
                    modulCode = Request.QueryString["Mod"];
                    loadModul(Session[CONST.SESSION_NEWS].ToString());
                }
                catch (Exception)
                {

                }
            }
        }

        protected void lnkDetailNews_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                Session[CONST.SESSION_NEWS] = Convert.ToInt32(e.CommandArgument);
                try
                {
                    modulCode = Request.QueryString["Mod"];
                    loadModul(Session[CONST.SESSION_NEWS].ToString());
                }
                catch (Exception)
                {

                }
            }
        }

        protected void imgNews_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                Session[CONST.SESSION_NEWS] = Convert.ToInt32(e.CommandArgument);
                try
                {
                    modulCode = Request.QueryString["Mod"];
                    loadModul(Session[CONST.SESSION_NEWS].ToString());
                }
                catch (Exception)
                {

                }
            }
        }

        protected void Pagination(int take)
        {
            try
            {
                categoryIDs = int.Parse(Request[CONST.QUERYSTRING_CATEGORY].ToString());

                if (TotalRow % take == 0)
                    TotalPage = TotalRow / take;
                else
                    TotalPage = (TotalRow / take) + 1;


                if (CurPage > 1)
                {

                    //lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(CurPage - 1) + "&Mod=" + modulCode;
                    lnkPrev.NavigateUrl = "~/Hem/tin-tuc/" + categoryIDs + "_" + modulCode + "_" + (CurPage - 1) + "/" + overServices.ConvertString(newsMana.GetNameCateByID(categoryIDs).Replace(" ","-")) + ".html";
                }
                if (CurPage < TotalPage && CurPage > 1)
                {

                    //lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_CATEGORY + "=" + categoryIDs + "&Page=" + Convert.ToString(CurPage + 1) + "&Mod=" + modulCode;
                    lnkNext.NavigateUrl = "~/Hem/tin-tuc/" + categoryIDs + "_" + modulCode + "_" + (CurPage + 1) + "/" + overServices.ConvertString(newsMana.GetNameCateByID(categoryIDs).Replace(" ","-")) + ".html";
                }

                if (CurPage == 1 && TotalPage == 1)
                {
                    lnkNext.Text = "";
                    lnkPrev.Text = "";
                    lnkNext.Visible = false;
                    lnkPrev.Visible = false;
                }
                else if (CurPage == 1 && TotalPage > 1)
                {
                    lnkPrev.Text = "";
                    lnkPrev.Visible = false;
                }
                else if (1 < CurPage && CurPage < TotalPage)
                {

                }
                else if (CurPage == TotalPage)
                {
                    lnkNext.Text = "";
                    lnkNext.Visible = false;
                }
            }
            catch (Exception)
            {

            }
        }

    }
}